package cn.edu.xjtu.android.emoji_warfare_android.game.hero;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import cn.edu.xjtu.android.emoji_warfare_android.engine.shape.Shape;
import cn.edu.xjtu.android.emoji_warfare_android.game.BulletsBuilder;
import cn.edu.xjtu.android.emoji_warfare_android.game.Config;
import cn.edu.xjtu.android.emoji_warfare_android.game.Engine;
import cn.edu.xjtu.android.emoji_warfare_android.game.item.SheildItem;
import cn.edu.xjtu.android.emoji_warfare_android.game.item.WeaponItem;

/**
 * Enemy<br>
 * 敌人
 * 
 * @author bill
 *
 */
public class Enemy extends Character {
    private boolean debug = false;
    /**
     * 表示敌人状态:0为刚入场, 1为在场上停滞 ,2表示正在出场
     * 
     * @author wyyf
     */
    public int status;
    /**
     * 在场上滞留时间
     * 
     * @author wyyf
     */
    public int stayTimes;

    public boolean inDamage;

    public int lasetDamageCount;

    private Engine engine;

    /**
     * 构造一个Enemy
     * 
     * @param level
     *            敌人等级
     * @param engine
     *            游戏引擎
     */
    public Enemy(int level, Engine engine) {
        super(Character.mRandom.nextInt(3) + 1);
        hp = (int) ((level + 5) * 2);
        this.engine = engine;
        this.level = level;

    }

    /**
     * 构造一个Enemy
     * 
     * @param outline
     *            碰撞箱形状
     */
    public Enemy(Shape outline) {
        super(outline);
    }

    @Override
    public boolean onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制受伤效果
        if (inDamage) {
            canvas.drawCircle(0, 0, Config.Hero.radius, paint);
            inDamage = false;
        }
        // debug用
        if (debug) {
            canvas.drawText(String.valueOf(hp), Config.Hero.radius - 20, -Config.Hero.radius, paint);
            canvas.drawText(String.valueOf(level), Config.Hero.radius - 20, 30 - Config.Hero.radius, paint);
        }
        return false;
    }

    /**
     * 掉落物品
     * 
     * @param index
     *            物品编号
     */
    public void dropItem(int index) {
        if (index < 4) {
            WeaponItem wi = new WeaponItem(basePoint.x, basePoint.y, engine.Render.getScreenBoundary(), index);
            engine.Layers.Item.add(wi);
            engine.Move.add(wi);
        } else {
            SheildItem si = new SheildItem(basePoint.x, basePoint.y, engine.Render.getScreenBoundary());
            engine.Layers.Item.add(si);
            engine.Move.add(si);
        }

    }

    /**
     * 发射子弹
     */
    public void fire() {
        engine.addEnemyBullets(BulletsBuilder.buildScattershot(basePoint.x, basePoint.y + Config.Hero.radius, 3,
                Config.Bullet.speed / 3, 60 + 15 * Character.mRandom.nextInt(5), 90, getBulletCount(), 1));
    }

    /**
     * 获取当前应该发射子弹的数目
     * 
     * @return 子弹数目
     */
    private int getBulletCount() {
        if (Engine.instance.el.waveIndex < 50) {
            return (Character.mRandom.nextInt(3) + 2);
        } else {
            return (int) ((Character.mRandom.nextInt(3) + 2) * (Engine.instance.el.waveIndex / 50f));
        }
    }

    public final static Paint paint;
    static {
        paint = new Paint();
        paint.setColor(Color.argb(64, 255, 0, 0));
        paint.setStyle(Style.FILL);
        paint.setAntiAlias(true);
        paint.setTextSize(30);
        paint.setStrokeWidth(2);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("class: ");
        sb.append(this.getClass().getName());
        sb.append(";");
        sb.append("appearance: ");
        sb.append((appearance == null) ? "NULL" : appearance.toString());
        sb.append(";");
        return sb.toString();
    }

}
